//
//  AskButtonView.m
//  DesighBook
//
//  Created by 梅明 on 15/9/19.
//  Copyright (c) 2015年 梅明. All rights reserved.
//

#import "AskButtonView.h"

#define viewwidth self.frame.size.width
@implementation AskButtonView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(void)setbuttonWithArray:(NSArray *)titlearray AndPage:(NSString *)page
{
    _page = page;
    _buttonwidth = self.frame.size.width/titlearray.count;
    for (int i = 0; i<titlearray.count; i++) {
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(_buttonwidth*i, 0, _buttonwidth, self.frame.size.height)];
        [button setTitle:titlearray[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        button.tag = 100 + i;
        if (i == 0) {
            button.selected = YES;
        }
        
        [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
    }
    
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, self.frame.size.height-1, viewwidth, 1)];
    view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    _downview = [[UIView alloc]initWithFrame:CGRectMake(0, 38, _buttonwidth, 2)];
    _downview.backgroundColor = [UIColor redColor];
    [self addSubview:view];
    [self addSubview:_downview];
    self.backgroundColor = [UIColor whiteColor];
    
}

-(void)click:(UIButton *)button
{
    if (_delegate && [_delegate respondsToSelector:@selector(askButtonView:flag:)]) {
        [_delegate askButtonView:self flag:button.tag-100];
    }
    
    for (UIButton *but in self.subviews) {
        if ([but isKindOfClass:[UIButton class]]) {
            but.selected = NO;
        }
    }
   
    button.selected = YES;
    
    if ([_page isEqualToString:@"ask"]) {
        //block传值
        self.block((int)button.tag);
    }
    
    [UIView animateWithDuration:0.2 animations:^{
        
        CGRect frame = _downview.frame;
        if (_downview.frame.origin.x/(_buttonwidth)>(button.tag-100)) {
            frame.origin.x = (button.tag-100)*_buttonwidth-10;
            _downview.frame = frame;
        }else {
            frame.origin.x = (button.tag-100)*_buttonwidth+10;
            _downview.frame = frame;
        }
        
    } completion:^(BOOL finished) {
        CGRect frame = _downview.frame;
        frame.origin.x =(button.tag-100)*_buttonwidth;
        _downview.frame = frame;
        
    }];
    
//    if ([_page isEqualToString:@"scroll"]) {
//        //通知中心传参
//        [[NSNotificationCenter defaultCenter]postNotificationName:@"postValue" object:button];
//    }
   
//    [_downview removeFromSuperview];
//    _downview = [[UIView alloc]initWithFrame:CGRectMake(viewwidth/3*(button.tag-100), 38, viewwidth/3, 2)];
//    _downview.backgroundColor = [UIColor redColor];
//    [self addSubview:_downview];
    


}
@end
